package com.edushare.common.dao;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import org.apache.cayenne.access.DataContext;
import org.apache.cayenne.exp.Expression;
import org.apache.cayenne.query.Ordering;
import org.apache.cayenne.query.SelectQuery;
import org.apache.cayenne.query.SortOrder;

import com.edushare.common.orm.ORMInterface;
import com.edushare.common.vo.OrgAliases;
import com.edushare.common.vo.ProfessionalDetails;
import com.edushare.common.vo.Users;

public class ProfessionalDetailsDAO extends CayenneBasedDAO implements Serializable {

	private static final long serialVersionUID = 8774516910410517198L;

	public ProfessionalDetailsDAO(ORMInterface ormInterface) {
		super(ormInterface);
	}
	
	@SuppressWarnings("unchecked")
	public List<ProfessionalDetails> getPositions(Users user){
		
		DataContext dc = getDataContext();
		
		List<ProfessionalDetails> positions = null;
		Expression exp = Expression.fromString("user = $user and proDetailType = $proType");
		HashMap hm = new HashMap();
		hm.put("user", user);
		hm.put("proType", ProfessionalDetails.PRO_DETAIL_TYPE_POSITION);
		SelectQuery query = new SelectQuery(ProfessionalDetails.class, exp.expWithParameters(hm));
		positions = dc.performQuery(query);
		List<Ordering> orderings = new ArrayList<Ordering>();
		orderings.add(new Ordering("toDate",SortOrder.DESCENDING));
		orderings.add(new Ordering("fromDate", SortOrder.DESCENDING));
		Ordering.orderList(positions, orderings);
		
		return positions;
	}

	public void delete(ProfessionalDetails proDetails) {
		
		DataContext dc = getDataContext();
		dc.commitChanges();
		dc.deleteObject(proDetails);
		dc.commitChanges();
	}

	public void saveOrUpdate(ProfessionalDetails proDetails, Users user) {
		getDataContext().registerNewObject(proDetails);
		//See if organization name already exists
//		OrgAliases orgAlias = new OrgAliasesDAO(ormInterface).AddOrUpdate(proDetails.getOrganizationNameAddedByUser());
//		proDetails.setOrgAlias(orgAlias);
		proDetails.setUser(user);
	}

	@SuppressWarnings("unchecked")
	public List<ProfessionalDetails> getdegrees(Users user) {
		DataContext dc = getDataContext();
		
		List<ProfessionalDetails> positions = null;
		Expression exp = Expression.fromString("user = $user and proDetailType = $proType");
		HashMap hm = new HashMap();
		hm.put("user", user);
		hm.put("proType", ProfessionalDetails.PRO_DETAIL_TYPE_DEGREE);
		SelectQuery query = new SelectQuery(ProfessionalDetails.class, exp.expWithParameters(hm));
		positions = dc.performQuery(query);
		List<Ordering> orderings = new ArrayList<Ordering>();
		orderings.add(new Ordering("toDate",SortOrder.DESCENDING));
		orderings.add(new Ordering("fromDate", SortOrder.DESCENDING));
		Ordering.orderList(positions, orderings);
		
		return positions;
	}

	public List<ProfessionalDetails> getCurrentPositionsAndDegrees(Users user) {
		DataContext dc = getDataContext();
		
		List<ProfessionalDetails> positions = null;
		Expression exp = Expression.fromString("user = $user and isCurrent = $curr");
		HashMap hm = new HashMap();
		hm.put("user", user);
		hm.put("curr", new Boolean(true));
		SelectQuery query = new SelectQuery(ProfessionalDetails.class, exp.expWithParameters(hm));
		positions = dc.performQuery(query);
		List<Ordering> orderings = new ArrayList<Ordering>();
		orderings.add(new Ordering("toDate",SortOrder.DESCENDING));
		orderings.add(new Ordering("fromDate", SortOrder.DESCENDING));
		Ordering.orderList(positions, orderings);
		
		return positions;
	}

}
